//
//  CZEmoticonToolBar.swift
//  weibo
//
//  Created by Apple on 16/3/19.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

enum EmoticonType:Int {
    case Recent = 1
    case Default = 2
    case Emoji = 3
    case Lxh = 4
}

class CZEmoticonToolBar: UIStackView {

    //  保存上次点击的按钮
    private var currentButton: UIButton?
    var emoticonTypeSelectedClosure: ((type: EmoticonType)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //
    private func setupUI() {
        //  设置排列方向
        axis = .Horizontal
        //  布局方式
        distribution = .FillEqually
        
        //  设置按钮的拉伸方向
        addChildButton("最近", bgImageName: "compose_emotion_table_left", type: EmoticonType.Recent)
        addChildButton("默认", bgImageName: "compose_emotion_table_mid", type: EmoticonType.Default)
        addChildButton("Emoji", bgImageName: "compose_emotion_table_mid", type: EmoticonType.Emoji)
        addChildButton("浪小花", bgImageName: "compose_emotion_table_right", type: EmoticonType.Lxh)

        
        
    }
    
    private func addChildButton(title: String, bgImageName: String, type: EmoticonType) {
        let button = UIButton()
        button.addTarget(self, action: "childButtonClick:", forControlEvents: .TouchUpInside)
        button.tag = type.rawValue
        button.setBackgroundImage(UIImage(named: bgImageName + "_normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: bgImageName + "_selected"), forState: .Selected)
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Selected)
        button.titleLabel?.font = UIFont.systemFontOfSize(12)
        //  取消高亮效果
        button.adjustsImageWhenHighlighted = false
        //  添加按钮
        addArrangedSubview(button)
        
    }
    
    
    @objc private func childButtonClick(button: UIButton) {
//        currentButton?.selected = false
//        button.selected = true
//        currentButton = button
        if button == currentButton {
            return
        }
        selectButton(button)
        emoticonTypeSelectedClosure?(type: EmoticonType(rawValue: button.tag)!)
    }
    
    func showSelectButton(section: Int) {
        //  如果出入的section + 1 等于 上次保存的按钮的tag 不需要查找爬到你
        if section + 1 == currentButton?.tag {
            return
        }
//        //  上次保存的按钮取消
//        currentButton?.selected = false
//        //  根据tag获取button
//        let button = viewWithTag(section + 1) as! UIButton
//        button.selected = true
//        currentButton = button
        //  根据tag获取button
        let button = viewWithTag(section + 1) as! UIButton
        selectButton(button)
    }
    //  抽取选中的按钮
    private func selectButton(button: UIButton) {
        currentButton?.selected = false
        button.selected = true
        currentButton = button
    }
}
